package com.mobox.uav.view.activity

import android.annotation.SuppressLint
import android.graphics.Color
import android.graphics.drawable.ColorDrawable
import android.support.v7.app.AppCompatActivity
import android.os.Bundle
import android.view.Gravity
import android.view.LayoutInflater
import android.view.WindowManager
import android.widget.LinearLayout
import android.widget.PopupWindow
import com.mobox.uav.R
import kotlinx.android.synthetic.main.activity_main.*
import kotlinx.android.synthetic.main.include_toplayout.*
import kotlinx.android.synthetic.main.layout_moresetting.view.*
import kotlinx.android.synthetic.main.layout_setting.view.*
import android.os.Build
import android.view.View
import android.view.animation.Animation
import android.view.animation.AnimationUtils
import com.mobox.uav.persenter.MainImpl
import kotlinx.android.synthetic.main.layout_moresetting.*
import kotlinx.android.synthetic.main.layout_setting.*


class MainActivity : AppCompatActivity() {

    var popupWindow: PopupWindow? = null
    var moresettingWindow: PopupWindow? = null
    val mainImpl = MainImpl(this)

    var animIn: Animation? = null
    var animOut: Animation? = null
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        hideBottomUIMenu()
        setContentView(R.layout.activity_main)
        initSettingAnimView()
        initListener()
    }

    private fun initSettingAnimView() {
        animIn = AnimationUtils.loadAnimation(this, R.anim.pop_right_in)
        animOut = AnimationUtils.loadAnimation(this, R.anim.pop_right_out)
    }

    private fun initListener() {
        layoutSetting.setOnClickListener {}
        moreSetting.setOnClickListener {}
        //用户相关
        iv_user.setOnClickListener {}
        //奖励
        iv_welfare.setOnClickListener {}
        //排名
        iv_ranking.setOnClickListener {}
        //设置
        iv_setting.setOnClickListener {
            //            showSetting()
            mainImpl.settingIn(v_bg, layoutSetting, animIn!!)
        }
        //起飞
        iv_launch.setOnClickListener {}
        //媒体（图片与视频）
        iv_file.setOnClickListener {}
        /**（isSelected = true ：拍照）（isSelected = false ：摄像）*/
        iv_switch.isSelected = true
        iv_switch.setOnClickListener {
            if (iv_switch.isSelected) {
                iv_switch.isSelected = false
                iv_switch.setImageResource(R.drawable.btn_video)
                iv_kaka.setImageResource(R.mipmap.status_video)
            } else {
                iv_switch.isSelected = true
                iv_switch.setImageResource(R.drawable.btn_camera)
                iv_kaka.setImageResource(R.mipmap.status_camera)
            }
        }
        iv_kaka.setOnClickListener {}
        /** MoreSetting 事件监听------------------------------------------ */
        arl_moresetting.setOnClickListener {
            val anim = AnimationUtils.loadAnimation(this, R.anim.pop_right_in)
            mainImpl.settingIn(v_bg, moreSetting, anim)
        }
        /**消失setting*/
        all_back.setOnClickListener { mainImpl.settingOut(layoutSetting, moreSetting, v_bg, animOut!!) }
        v_bg.setOnClickListener { mainImpl.settingOut(layoutSetting, moreSetting, v_bg, animOut!!) }
        /** 动画监听*/
        animOut!!.setAnimationListener(object : Animation.AnimationListener {
            override fun onAnimationRepeat(p0: Animation?) {

            }

            override fun onAnimationEnd(p0: Animation?) {
                if (moreSetting.visibility == View.VISIBLE)
                    moreSetting.visibility = View.GONE
                else if (layoutSetting.visibility == View.VISIBLE)
                    layoutSetting.visibility = View.GONE
            }

            override fun onAnimationStart(p0: Animation?) {
            }
        })

    }

    fun showSetting() {
        val settingView = LayoutInflater.from(this@MainActivity).inflate(R.layout.layout_setting, null)
        settingView.arl_moresetting.setOnClickListener { moreSetting() }
        popupWindow = PopupWindow(settingView, LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.MATCH_PARENT, true)
        popupWindow?.animationStyle = R.style.pop_animation
        popupWindow?.setBackgroundDrawable(ColorDrawable(Color.TRANSPARENT))
        popupWindow?.isOutsideTouchable = true
        popupWindow?.softInputMode = WindowManager.LayoutParams.SOFT_INPUT_ADJUST_RESIZE
        popupWindow?.isTouchable = true
        popupWindow?.showAtLocation(window.decorView, Gravity.RIGHT, 0, 0)
    }

    fun moreSetting() {

        val settingView = LayoutInflater.from(this@MainActivity).inflate(R.layout.layout_moresetting, null)

        settingView.all_back.setOnClickListener {
            if (moresettingWindow != null) {
                moresettingWindow?.dismiss()
            }
        }
        moresettingWindow = PopupWindow(settingView, LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.MATCH_PARENT, true)
        moresettingWindow?.animationStyle = R.style.pop_animation
        moresettingWindow?.setBackgroundDrawable(ColorDrawable(Color.TRANSPARENT))
        moresettingWindow?.isOutsideTouchable = true
        moresettingWindow?.isTouchable = true
        popupWindow?.softInputMode = WindowManager.LayoutParams.SOFT_INPUT_ADJUST_RESIZE
        moresettingWindow?.showAtLocation(parentView, Gravity.RIGHT, 0, 0)
        moresettingWindow?.setOnDismissListener {
            popupWindow?.showAtLocation(window.decorView, Gravity.RIGHT, 0, 0)
        }
    }

    protected fun hideBottomUIMenu() {
        window.addFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN)
        //隐藏虚拟按键，并且全屏
        if (Build.VERSION.SDK_INT in 12..18) { // lower api
            val v = this.window.decorView
            v.systemUiVisibility = View.GONE
        } else if (Build.VERSION.SDK_INT >= 19) {
            //for new api versions.
            val decorView = window.decorView
            val uiOptions = (View.SYSTEM_UI_FLAG_HIDE_NAVIGATION
                    or View.SYSTEM_UI_FLAG_IMMERSIVE_STICKY or View.SYSTEM_UI_FLAG_FULLSCREEN)
            decorView.systemUiVisibility = uiOptions
        }
    }
}
